package com.enjoyor.soa.traffic.server.tsms.helper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.enjoyor.soa.traffic.api.dto.idcs.VehiclePassDto;
import com.enjoyor.soa.traffic.api.service.dubbo.idcs.IDCSCallbackListener;
import com.enjoyor.soa.traffic.server.tsms.common.RehearseGlobal;
import com.enjoyor.soa.traffic.server.tsms.handler.InitializeHandler;
import com.enjoyor.soa.traffic.util.handler.BasicMsgHandler;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

public class VehiclePassListenerImpl implements IDCSCallbackListener {
	
	// 全局变量
	private static VehiclePassListenerImpl IDCSListener= null;
	/**
	 * 实体对象
	 * @return
	 */
	public static VehiclePassListenerImpl getInstance() {						
		if (IDCSListener == null) {
			IDCSListener = new VehiclePassListenerImpl() ;			 
		}
		return IDCSListener;
	}
	
	@SuppressWarnings("unchecked")
	@Override
    public void changed(ResultPojo msg) {
		try {
			System.out.println("订阅过车数据-----------------"+msg);
			List<VehiclePassDto> list = (List<VehiclePassDto>) msg.getResultList();
			handler.handle(list);
		} catch (Exception e) { 
			//logger.error("转换定位对象或保存定位异常，详细：" + e.getMessage());
		}	
	}
	@SuppressWarnings("rawtypes")
	private BasicMsgHandler handler = new BasicMsgHandler<List<VehiclePassDto>>() {
		@Override
		protected boolean process(List<VehiclePassDto> list) {
			for (VehiclePassDto dto : list) {
				for (String key : RehearseGlobal.ActionDataPool.keySet()) {
		            if (RehearseGlobal.ActionDataPool.get(key).get("beforeLicensePlate") != null) {
		                if (RehearseGlobal.ActionDataPool.get(key).get("beforeLicensePlate").toString().equals(dto.getVehicle_plate())) {
		                	//推送过车数据
		    				com.alibaba.fastjson.JSONObject jsonObject2 = new com.alibaba.fastjson.JSONObject();
		    				Map<String, Object> vehicleMap = new HashMap<String, Object>();
		    				vehicleMap.put("type", "vehicle");
		    				vehicleMap.put("recordTime", TimeHelper.dateToLongString());
		    				vehicleMap.put("bayonetId", dto.getBayonet_id());
		    				//......过车具体信息待完善
		    				jsonObject2.put(RehearseGlobal.ActionDataPool.get(key).get("routeId").toString(), vehicleMap);
		    				InitializeHandler.getInstance().getServerPushServiceImpl().pushMessage("" + key, jsonObject2);
		                }
		            }
				}
			}
			return true;
		}
	};
}
